#include "Gomoku.h"
#include "Board.h"
#include "PlayerTurnPrompter.h"

PlayerTurnPrompter& PlayerTurnPrompter::m_instance = PlayerTurnPrompter();

PlayerTurnPrompter::PlayerTurnPrompter()
: m_font(NULL), m_count(0)
{
}

PlayerTurnPrompter::~PlayerTurnPrompter()
{
}

PlayerTurnPrompter& PlayerTurnPrompter::Instance()
{
	return (PlayerTurnPrompter::m_instance);
}

bool PlayerTurnPrompter::init()
{
	m_font = TTF_OpenFont("fonts/shanghai.ttf", 70);
	return (true);
}

bool PlayerTurnPrompter::prompt(SDL_Surface* _screen)
{
	if (m_count != 0)
	{
		m_text.Position.x = _screen->w / 2 - m_text.Surface->w / 2;
		m_text.Position.y = _screen->h / 2 - m_text.Surface->h / 2;
	}
	SDL_BlitSurface(m_text.Surface, NULL, _screen, &m_text.Position);
	return (true);
}

void PlayerTurnPrompter::setCount(unsigned int _count)
{
	m_count = _count;
}

void PlayerTurnPrompter::decreaseCount(unsigned int _value)
{
	m_count -= _value;
	if (m_count == 0)
	{
		if (m_text.Surface != NULL)
		{
			SDL_FreeSurface(m_text.Surface);
			m_text.Surface = NULL;
		}
	}
}

unsigned int PlayerTurnPrompter::getCount() const
{
	return (m_count);
}

void PlayerTurnPrompter::setText(std::string const& _text, BoardCase _color)
{
	if (m_text.Surface == NULL)
	{
		std::string text = _text;
		SDL_Color color;
		if (_color == COLOR_BLACK)
		{
			color.r = 0; color.g = 0; color.b = 0;
		}
		else
		{
			color.r = 255; color.g = 255; color.b = 255;
		}
		m_text.Surface = TTF_RenderText_Blended(m_font, text.c_str(), color); 
	}
}